﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.IO;

using System.Data;
using System.Collections;
namespace Client.Controllers
{
    public class TrainRocedController : Controller
    {
        //
        // GET: /TrainRoced/
        /// <summary>
        /// 调用服务
        /// </summary>
        TrainServiceMvc.TrainServiceSoapClient ServiceClient = new TrainServiceMvc.TrainServiceSoapClient();
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.coun = ServiceClient.ItemCount();
            return View();
        }
        public ActionResult Index2()
        {
            ViewBag.coun = ServiceClient.ItemCount();
            return View();
        }
        /// <summary>
        /// 记录页面
        /// </summary>
        /// <returns></returns>

        public ActionResult TrainRocedAdd()
        {

            List<TrainServiceMvc.Dept_Model> dep = ServiceClient.DepList().ToList();
            List<TrainServiceMvc.EmployeeLoginInfo_Model> EmpLog = ServiceClient.EmployeeLoginInfoList().ToList();




            List<TrainServiceMvc.TrainTypeModel> TrainTypeList = ServiceClient.TrainTypeList().ToList();
            List<TrainServiceMvc.TrainMethodModel> TrainMethodList = ServiceClient.TrainMethodList().ToList();
            List<TrainServiceMvc.TrainNatureModel> TrainNatureList = ServiceClient.TrainNatureList().ToList();

            SelectList TrainType_Id = new SelectList(TrainTypeList, "TrainType_Id", "TrainType_Name");
            SelectList TrainNature_Id = new SelectList(TrainNatureList, "TrainNature_Id", "TrainNature_Name");
            SelectList TrainMethod_Id = new SelectList(TrainMethodList, "TrainMethod_Id", "TrainMethod_Name");
            SelectList dep1 = new SelectList(dep, "DeptID", "DeptName");
            SelectList EmpLog1 = new SelectList(EmpLog, "EmployID", "EmployName");
            ViewBag.TrainType_Id = TrainType_Id;
            ViewBag.TrainNature_Id = TrainNature_Id;
            ViewBag.TrainMethod_Id = TrainMethod_Id;
            ViewBag.Sponsor_Id = dep1;

            ViewBag.PropleId = EmpLog1;

            return View();
        }
        public ActionResult TrainInfo()
        {
            List<TrainServiceMvc.TrainTypeModel> TrainTypeList = ServiceClient.TrainTypeList().ToList();
            List<TrainServiceMvc.TrainMethodModel> TrainMethodList = ServiceClient.TrainMethodList().ToList();
            List<TrainServiceMvc.TrainNatureModel> TrainNatureList = ServiceClient.TrainNatureList().ToList();

            SelectList TrainType_Id = new SelectList(TrainTypeList, "TrainType_Id", "TrainType_Name");
            SelectList TrainNature_Id = new SelectList(TrainNatureList, "TrainNature_Id", "TrainNature_Name");
            SelectList TrainMethod_Id = new SelectList(TrainMethodList, "TrainMethod_Id", "TrainMethod_Name");
            ViewBag.TrainType_Id = TrainType_Id;
            ViewBag.TrainNature_Id = TrainNature_Id;
            ViewBag.TrainMethod_Id = TrainMethod_Id;
            return View();
        }
        public ActionResult TrainRocedAddTo(TrainServiceMvc.TrainRecordModel s1)
        {


            s1.TrainRecord_BeginTime = DateTime.Now;
            var SS = Request["TimeText"];
            int adds = Convert.ToInt32(SS);
            s1.TrainRecord_OverTime = s1.TrainRecord_BeginTime.AddDays(adds);
            s1.TrainRecord_GUId = Guid.NewGuid().ToString();

            var i = ServiceClient.TrainRecordAdd(s1);
            if (i > 0)
            {
                return Content("<script>alert('添加成功！');Location.href='/TrainRoced/TrainRocedAdd'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败！');Location.href='/TrainRoced/TrainRocedAdd'</script>");

            }


        }

        public string JsonString()
        {
            List<TrainServiceMvc.ItembankModel> item = ServiceClient.ItembankList().ToList();
            List<TrainServiceMvc.ResultModel> result = ServiceClient.ResultList().ToList();
            

            StringBuilder json = new StringBuilder();
            json.Append("{");
            json.Append("\"Ti\":[");
            for (int i = 0; i < item.Count; i++)
            {
                json.Append("{");
                json.Append("\"ItemId\":\"" + item[i].ItemId + "\",");
                json.Append("\"ItemName\":\"" + item[i].ItemName + "\",");
                json.Append("\"ItemProId\":\"" + item[i].ItemProId + "\",");
                json.Append("\"Ti\":[");

                //动态拼接选项
                ArrayList fuhao = new ArrayList();
                fuhao.Add("A");
                fuhao.Add("B");
                fuhao.Add("C");
                fuhao.Add("D");
                fuhao.Add("E");
                for (int j = 0; j < result.Count; j++)
                {
                    
                    if (item[i].ItemProId == result[j].ItemId)
                    {
                        json.Append("{");

                        json.Append("\"fuhao\":\"" + fuhao[0] + "\",");
                        fuhao.Remove(fuhao[0]);
                        
                        json.Append("\"ResultId\":\"" + result[j].ResultId + "\",");
                        json.Append("\"ResultName\":\"" + result[j].ResultName + "\",");
                        json.Append("\"ItemId\":\"" + result[j].ItemId + "\",");
                        json.Append("\"ResulProId\":\"" + result[j].ResulProId + "\"");

                        json.Append("},");

                    }
                }

                json.Remove(json.Length - 1, 1);
                json.Append("]},");
            }

            json.Remove(json.Length - 1, 1);
            json.Append("]}");

            return json.ToString();
        }


        //考试页面！
        public ActionResult Testpage()
        {
            ViewBag.coun = ServiceClient.ItemCount();
            return View();
        }
        //计算答案得分
        public Double ExamResult(string ItemId, string ResultId)
        {


            var S1 = ServiceClient.ExamResult(ItemId, ResultId);
            var grade = S1 * 10;
            int IntItemCount = ServiceClient.ItemCount();

            ServiceReference1.EmployeeLoginInfo_Model login = Session["LoginInfo"] as ServiceReference1.EmployeeLoginInfo_Model;



            TrainServiceMvc.GradeModel Grad = new TrainServiceMvc.GradeModel();
            Grad.staff_Id = login.EmployID;
            Grad.Score1 = Convert.ToDecimal(grade);
            Grad.GradTime = DateTime.Now;
            if (grade > ((IntItemCount - 4) * 10))
            {
                Grad.Grad_ProId = 1;

            }
            else
            {
                Grad.Grad_ProId = 0;
            }
            ServiceClient.GardAdd(Grad);


            return Convert.ToDouble(grade);


        }
        //添加考试试题！
        public ActionResult Tolead()
        {
            return View();
        }

        //导入试题
        public ActionResult ToleadAdd(HttpPostedFileBase TestItem)
        {
            if (TestItem != null)
            {

                var im = Path.GetFileName(TestItem.FileName).ToString();
                var path = Path.Combine(Request.MapPath("~/Exal/") + im);
                TestItem.SaveAs(path);
                var s1 = ServiceClient.ItemBankAdd(path);
                if (s1 > 0)
                {
                    return Content("<script>alert('导入试题');</script>");
                }
                else
                {
                    return Content("<script>alert('导入试题失败');</script>");
                }
            }
            else
            {
                return Content("<script>alert('没有文件！');</script>");
            }



        }
        /// <summary>
        /// 试题列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DeleteItemList()
        {
            return View();
        }


        /// <summary>
        /// 删除试题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteItem(string id)
        {
            var i = ServiceClient.DeleteItemBank(id);
            if (i > 0)
            {
                return 1;

            }
            else
            {
                return 0;
            }
        }




        #region  服务器引用！
        /// <summary>
        /// 题库！
        /// </summary>
        /// <returns></returns>
        public string ItembankList1()
        {
            List<TrainServiceMvc.ItembankModel> ItembankList = ServiceClient.ItembankList().ToList();

            return JsonConvert.SerializeObject(ItembankList);
        }
        /// <summary>
        /// 结果运算！
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public string ResultList1()

        {



            var ResultList = ServiceClient.ResultList().ToList();


            return JsonConvert.SerializeObject(ResultList);
        }

        #endregion
        #region ajax调用前台拼接；

        public int TrainRecord_CostCount()
        {
            return ServiceClient.TrainRecord_CostCount();
        }
        public int PropleIdCount()
        {
            return ServiceClient.PropleIdCount();
        }





        public string TrainTypeList1()
        {
            List<TrainServiceMvc.TrainTypeModel> TrainTypeList = ServiceClient.TrainTypeList().ToList();

            return JsonConvert.SerializeObject(TrainTypeList);
        }
        public string TrainRecorLis1()
        {
            List<TrainServiceMvc.TrainRecordModel> TrainRecorLis = ServiceClient.RecordList().ToList();
            return JsonConvert.SerializeObject(TrainRecorLis);
        }
        public string TrainMethodList1()
        {
            List<TrainServiceMvc.TrainMethodModel> TrainMethodList = ServiceClient.TrainMethodList().ToList();

            return JsonConvert.SerializeObject(TrainMethodList);
        }
        public string TrainNatureList1()
        {
            List<TrainServiceMvc.TrainNatureModel> TrainNatureList = ServiceClient.TrainNatureList().ToList();

            return JsonConvert.SerializeObject(TrainNatureList);
        }



        #endregion
    }
}
